home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / TR2LATEX / c / tr2latex (.txt) < prev    next >
LaTeX Document  |  1992-05-16  |  8KB  |  246 lines

  1. ** tr2latex - troff to LaTeX converter
  2. ** $Id: tr2latex.c,v 2.2 1992/04/27 15:13:26 Christian_Engel Dist krischan $
  3. ** COPYRIGHT (C) 1987 Kamal Al-Yahya, 1991,1992 Christian Engel
  4. ** Module: tr2latex.c
  5. ** This module contains the main function inititating the translation
  6. ** and supporting the Usage.
  7. char *usage_doc[] = {
  8. "tr2latex - troff to LaTeX converter, $Revision: 2.2 $",
  9. "SYNTAX:  tr2latex [-m] [-t] [-<n>] [-s <style>] [-o <outfile>] [<file>...]",
  10. "options: -m            for manual",
  11. "         -t            twoside page style",
  12. "         -h            use helvetica style option",
  13. "         -<n>          a number n gives the font size (default is 12pt",
  14. "                       for man, 11pt otherwise)",
  15. "         -s <style>    use documentstyle <style> (default is article)",
  16. "         -o <outfile>  send output to <outfile> (default is stdout)",
  17. #ifdef DEBUG
  18. "         -do           debug output",
  19. "         -dv           verbose informations",
  20. #endif
  21. #define MAIN
  22. #include    "setups.h"
  23. #include    "protos.h"
  24. #ifdef __TURBOC__
  25. /*--- The Turbo C stack needs to be increased in size,
  26.       6k seems to be enough; 4k is not                 ---*/
  27. extern unsigned _stklen = 0x1800 ;
  28. #endif
  29. extern char *mktemp();
  30. char scratch_file[MAXWORD];
  31. bool    man,        /* option -m: manual page */
  32.         fontsize,    /* option -9/-10/-11/-12: font size */
  33.         twoside,    /* option -t: twoside */
  34.         piped_in,
  35.         helvetica;
  36. char *document = "article";    /* document type, see also -s option */
  37. extern char version [];
  38. FILE *out_file;        /* in case they can't use *NIX redirecting or piping */
  39. char *prgname;
  40. char inbuf[MAXLEN],
  41.      outbuf[MAXLEN];
  42. int main (int argc, char *argv[])
  43.     char *pin = inbuf,
  44.          *pout = outbuf;
  45.     FILE *in_file;
  46.     long timeval;        /* clock value from time() for ctime()    */
  47.     int argi;
  48.     prgname = argv [0];
  49.     out_file = stdout;        /* default output */
  50.     /* process option flags */
  51.     getopts (&argc, argv);
  52. #ifdef DEBUG
  53.     if (debug_v) {
  54.         printf ("Arguments:");
  55.         for (argi = 1; argi < argc; argi++)
  56.             printf (" %s", argv [argi]);
  57.         putchar ('\n');
  58. #endif
  59. #ifdef    VMS
  60.     if ((out_file == stdout) &&
  61.         ((out_file = freopen ("Sys$Output:", "w", stdout,
  62.                               "rat=cr", "rfm=var")) == NULL)) {
  63.         fprintf (stderr, "Can't reopen stdout\n");
  64.         errexit (GOOD);
  65. #endif
  66.     /* initialize spacing and indentation parameters */
  67.     strcpy(linespacing.def_units,"\\normalbaselineskip");
  68.     strcpy(linespacing.old_units,"\\normalbaselineskip");
  69.     strcpy(indent.def_units,"em");
  70.     strcpy(indent.old_units,"em");
  71.     strcpy(tmpind.def_units,"em");
  72.     strcpy(tmpind.old_units,"em");
  73.     strcpy(space.def_units,"\\baselineskip");
  74.     strcpy(space.old_units,"\\baselineskip");
  75.     strcpy(vspace.def_units,"pt");
  76.     strcpy(vspace.old_units,"pt");
  77.     linespacing.value = 1.;
  78.     linespacing.old_value = 1.;
  79.     indent.value = 0.;
  80.     indent.old_value = 0.;
  81.     tmpind.value = 0.;
  82.     tmpind.old_value = 0.;
  83.     space.value = 1.;
  84.     space.old_value = 1.;
  85.     vspace.value = 1.;
  86.     vspace.old_value = 1.;
  87.     linespacing.def_value = 0;
  88.     indent.def_value = 0;
  89.     tmpind.def_value = 0;
  90.     space.def_value = 1;
  91.     vspace.def_value = 1;
  92.     math_mode = 0;                    /* start with non-math mode */
  93.     de_arg = 0;                     /* not a .de argument */
  94.     /* start of translated document */
  95.     timeval = time(0);
  96.     fprintf (out_file,
  97. "%% -*-LaTeX-*-\n\
  98. %% Converted automatically from troff to LaTeX\n\
  99. %% by %s\n\
  100. %% on %s\
  101. %% tr2latex was written by Kamal Al-Yahya at Stanford University\n\
  102. %% (Kamal%%Hanauma@SU-SCORE.ARPA)\n\
  103. %% and substantially enhanced by Christian Engel at RWTH Aachen\n\
  104. %% (krischan@informatik.rwth-aachen.de).\n\
  105. %%\n\
  106. %% troff input file%s:%s",
  107.              version, ctime(&timeval),
  108.              argc>2?"s":"",
  109.              argc==1?" <stdin>":"");
  110.     for (argi = 1; argi < argc; argi++) {
  111.         if (strcmp (argv [argi], "-") == 0)
  112.             fprintf (out_file, " <stdin>");
  113.         else
  114.             fprintf (out_file, " %s", argv[argi]);
  115.     /* document style and options */
  116.     fprintf (out_file,"\n\n\\documentstyle[%s", man? "troffman": "troffms");
  117.     if (fontsize == 0 && !man)
  118.         fontsize = 11;
  119.     if (fontsize != 0)
  120.     fprintf (out_file,",%dpt", fontsize);
  121.     if (helvetica)
  122.         fputs (",helvetica", out_file);
  123.     if (twoside)
  124.         fputs (",twoside", out_file);
  125.     fprintf (out_file,"]{%s}\n\\begin{document}\n", document);
  126.     if (argc == 1)
  127.         process (stdin, "<stdin>", pin, pout);
  128.     else {
  129.         for (argv++; --argc; argv++) {
  130.             if (strcmp (*argv, "-") == 0)
  131.                 process (stdin, "<stdin>", pin, pout);
  132.             else if ((in_file = fopen(*argv,"r")) == NULL)
  133.                 fprintf(stderr,"%s: Cannot open input file `%s'\n",
  134.                         prgname,*argv);
  135.             else {
  136.                 process (in_file, *argv, pin, pout);
  137.                 fclose(in_file);
  138.     /* close translated document */
  139.     fputs("\\end{document}\n",out_file);
  140.     exit(GOOD);
  141. void process (FILE *in_file, char *f_name, char *pin, char *pout)
  142.     static char sep[] = "--------------------------------------------------";
  143.     tmpbuf (in_file, pin);
  144.     fprintf (out_file, "%%%s\n%% start of input file: %s\n%%\n", sep, f_name);
  145.     troff_tex (pin, pout, 0, 0);
  146.     fputs (pout, out_file);
  147.     fprintf (out_file, "%%\n%% end of input file: %s\n%%%s\n", f_name, sep);
  148. # define shift_arg(cnt)         for (i = argind; i < *p_argc - cnt; i++)    \
  149.                                     argv [i] = argv [i + cnt];              \
  150.                                 argind -= cnt;                              \
  151.                                 *p_argc -= cnt;
  152. void getopts (int *p_argc, char *argv[])
  153.     char *p_opt;
  154.     int argind, i;
  155.     for (argind = 1; argind < *p_argc; argind ++)
  156.         if (*argv [argind] == '-') {
  157.             if (strcmp (argv+1, "help") == 0)
  158.                 usage (0);
  159.             for (p_opt = argv [argind] + 1; ; p_opt++) {
  160.                 switch (*p_opt) {
  161.                 case '\0':
  162.                     if (p_opt != argv [argind] + 1) {
  163.                         shift_arg (1);
  164.                     }
  165.                     break;
  166.                 case 'h':
  167.                     helvetica = 1;
  168.                     continue;
  169.                 case 'm':
  170.                     man = 1;
  171.                     continue;
  172.                 case 't':
  173.                     twoside = 1;
  174.                     continue;
  175.                 case 's':
  176.                     document = argv [argind+1];
  177.                     shift_arg(2);
  178.                     continue;
  179.                 case '9':
  180.                     fontsize = 9;
  181.                     continue;
  182.                 case 'o':
  183.                     if (argind + 1 >= *p_argc) {
  184.                         fprintf (stderr, "%s: Missing output file name\n",
  185.                                  prgname);
  186.                         usage (1);
  187.                     }
  188. #ifdef    VMS
  189.                     if ((out_file = fopen(argv[argind+1],"w",
  190.                                           "rat=cr","rfm=var")) == NULL)
  191. #else
  192.                     if ((out_file = fopen(argv[argind+1],"w")) == NULL)
  193. #endif
  194.                     {
  195.                         fprintf(stderr, "%s: can't open %s\n",
  196.                           prgname, argv [argind+1]);
  197.                         usage (errno);
  198.                     }
  199.                     shift_arg(2);
  200.                     break;
  201. #ifdef DEBUG
  202.                 case 'd':
  203.                     if (*++p_opt == '\0') {
  204.                         fprintf (stderr, "%s: missing debug option\n",
  205.                                  prgname);
  206.                         usage (1);
  207.                     }
  208.                     switch (*p_opt) {
  209.                     case 'o':
  210.                         debug_o = 1;
  211.                         break;
  212.                     case 'v':
  213.                         debug_v = 1;
  214.                         break;
  215.                     default:
  216.                         fprintf (stderr, "%s: unknown debug option %c\n",
  217.                                  prgname, *p_opt);
  218.                         usage (1);
  219.                     }
  220.                     continue;
  221. #endif
  222.                 case '?':
  223.                     usage (0);
  224.                 case '1':
  225.                     if (isdigit (p_opt [1])) {
  226.                         fontsize = 10 + *++p_opt - '0';
  227.                         continue;
  228.                     }
  229.                     /* NO BREAK */
  230.                 default:
  231.                     fprintf (stderr,"%s: Unknown option %c\n",prgname,*p_opt);
  232.                     usage (1);
  233.                 }
  234.                 break;
  235.             }
  236. void usage (int exitcode)
  237.     int i;
  238.     printf ("tr2latex (c) 1986/1987 Kamal Al-Yahya, 1991 C. Engel\nVersion %s\n",
  239.             version);
  240.     for (i=0; usage_doc [i]; i++)
  241.         printf ("%s\n", usage_doc [i]);
  242.     exit (exitcode);
  243. void errexit (int exitcode)
  244.     fprintf (stderr, "%s: Error #%03d ", prgname, exitcode);
  245.     exit (exitcode);
  246.